import type { IOrg, PList, POrgLoad } from '@/types/bo/OrgBo';
import * as OrgApi from '@/api/OrganizationApi';

const responseCache = new Map<string, any>();

export function onOrgLoad(data: POrgLoad) {
  return OrgApi.onOrgLoad(data);
}

export function getOrgInfo(data: POrgLoad): IOrg {
  return OrgApi.getOrgInfo(data);
}

export function onOrgCreate(data: IOrg) {
  return OrgApi.onOrgCreate(data);
}

export function onOrgUpdate(data: IOrg) {
  return OrgApi.onOrgUpdate(data);
}

export function fetchOrgBizList(data: PList) {
  return OrgApi.fetchOrgBizList(data);
}

export function getCommonData(data: any, cached: boolean) {
  let res;
  if (cached) {
    if (responseCache.has(JSON.stringify(data))) {
      return Promise.resolve(responseCache.get(JSON.stringify(data)));
    } else {
      res = OrgApi.getCommonData(data).then((result) => {
        if (result && result.data && result.data.records && result.data.records.length > 0) {
          responseCache.set(JSON.stringify(data), result);
        }
        return result;
      });
    }
  } else {
    res = OrgApi.getCommonData(data);
  }
  return res;
}
